<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数组的扁平化</title>
</head>
<body>

    <script>

        // 数组扁平化就是将多维数组变成一维数组 比如[1,[1,2],[1,2,3,4]]
        
        function fn (arr) {
            var ary = arr.toString().split(",").map(item => parseInt(item));
            return unique(ary).sort((a,b)=>a-b);
        }

        function unique (arr) {
            var obj = {};
            for(var i=0;i<arr.length;i++){
                var ele = arr[i];
                if(typeof obj[ele] != 'undefined'){
                    arr[i]=arr[arr.length - 1];
                    arr.length --;
                    i --;
                    continue;
                }
                obj[ele] = ele;
            }
            obj = null;
            return arr;
        }

        console.log(fn ([1,2,[1,2],[3,4,5]]));
    </script>
</body>
</html>